package com.yuyue.springFramework.note07;

import com.yuyue.springFramework.note07.entity.*;
import org.junit.Test;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

/**
 * 《Spring-7 @Conditional 注解》文章代码
 *  文章地址：https://mp.weixin.qq.com/s/UXCqCejGdwPziSUoO-20Og
 */
public class Note07Test {
    @Test
    //不注册配置类
    public void fun1(){
        //获取上下文对象
        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(Configuration1.class);
        for (String name: applicationContext.getBeanDefinitionNames()) {
            //仅打印自定义的类
            if(!name.startsWith("org.")){
                System.out.println(applicationContext.getBean(name));
            }
        }
    }

    @Test
    //注册配置类，不注册bean
    public void fun2(){
        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(Configuration2.class);
        for (String name: applicationContext.getBeanDefinitionNames()) {
            //仅打印自定义的类
            if(!name.startsWith("org.")){
                System.out.println(applicationContext.getBean(name));
            }
        }
    }

    @Test
    //同一个接口下，bean 不存在的时候才注册
    public void fun3(){
        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(Configuration5.class);
        for (String name: applicationContext.getBeanDefinitionNames()) {
            //仅打印自定义的类
            if(!name.startsWith("org.")){
                System.out.println(applicationContext.getBean(name));
            }
        }
    }

    @Test
    //使用在自定义注解上，通过 EnvCondition 设定不同环境
    public void fun4(){
        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(Configuration6.class);
        //获取数据源bean
        String dataSourse = applicationContext.getBean("dataSource", String.class);
        System.out.println(dataSourse);
    }

    @Test
    //同一个接口下，bean 不存在的时候才注册
    public void fun5(){
        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(Configuration7.class);
    }

    @Test
    //验证 Condition 条件判断会同时作用于 bean 的解析和注册
    //当解析时，因bean还未注册，导致即使bean的注册在bean是否存在判断之前，依然无法生效
    public void fun6(){
        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(Configuration10.class);
        for (String name: applicationContext.getBeanDefinitionNames()) {
            //仅打印自定义的类
            if(!name.startsWith("org.")){
                System.out.println(applicationContext.getBean(name));
            }
        }
    }

    @Test
    //验证 ConfigurationCondition 指定bean注册阶段，使得条件注解生效
    public void fun7(){
        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(Configuration12.class);
        for (String name: applicationContext.getBeanDefinitionNames()) {
            //仅打印自定义的类
            if(!name.startsWith("org.")){
                System.out.println(applicationContext.getBean(name));
            }
        }
    }
}
